import { defineStore } from "pinia";
import { ref } from "vue";
export const useUserStore = defineStore( 'user', ( ) => {
	const userInfo = ref( {
		uid: '',
		username: '游客', //平台随机用户名
		mobile: '',
		role: [ 'user' ]
	} )
	// 更新用户信息
	const setUserinfo = ( obj ) => {
		userInfo.value = { ...userInfo.value, ...obj }
	}

	// 清除用户信息
	const clearnUserInfo = ( ) => {
		// 恢复为未登录的默认值
		userInfo.value = {
			uid: '',
			username: '游客', //平台随机用户名
			mobile: '',
			role: [ 'user' ]
		}
	}

	return {
		userInfo,
		setUserinfo,
		clearnUserInfo
	}
}, {
	// 网页端配置
	// persist: true,

	// 小程序端配置
	persist: {
		storage: {
			getItem( key ) {
				return uni.getStorageSync( key )
			},
			setItem( key, value ) {
				uni.setStorageSync( key, value )
			},
		},
	},
} )